package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.crm.entity.GwecT;
import cn.e4j.modular.crm.service.Gwei005Service;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

/**
 * 出货信息维护控制类
 *
 * @author mengyb
 * @Date 2019-05-08
 */
@Controller
@RequestMapping("/crm/gwei005")
public class Gwei005Controller extends BaseController {

    @Autowired
    private Gwei005Service gwei005Service;

    @Autowired
    private ErpLogic erpLogic;


    /**
     * 跳转页面
     */
    @RequiresPermissions("crm:gwei005:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwei005.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser= ShiroKit.getUser();
        inDto.put("ent", shiroUser.getDBC());
        inDto.put("USER_ID", shiroUser.getID());

        List<Dto> resultList = gwei005Service.list_(inDto, page);
        for (int i = 0; i < resultList.size(); i++) {
//            if(StringUtils.isNotEmpty(resultList.get(i).getString("GWEC003"))){
//                Dto eDto= Dtos.newDto();
//                eDto.put("OEB01",resultList.get(i).getString("GWEC003"));//订单号
//                eDto.put("OEB03",resultList.get(i).getBigDecimal("GWEC101"));//订单项次
//                eDto.put("DBC",resultList.get(i).getString("GWECPLANT"));
//                Dto oeb =gwei005Service.getOeb(eDto);
//                if(StringUtils.isNotEmpty(oeb)){
//                    resultList.get(i).put("GWCS002",oeb.getString("OEB05"));//单位
//                    resultList.get(i).put("GWCS003",oeb.getBigDecimal("OEB12"));//数量
//                }
//            }
            //计算已出货数量
            Dto dto = Dtos.newDto();
            dto.put("ent", shiroUser.getDBC());
            dto.put("GWEC003", resultList.get(i).getString("GWEC003"));
            dto.put("GWEC101", resultList.get(i).getString("GWEC101"));
            List<Dto> list = gwei005Service.listCount(dto);//针对订单单号与订单项次的一组出货单集合
            if (list.size() == 0) {
                resultList.get(i).put("COUNT", 0);
            } else {
                BigDecimal COUNT = new BigDecimal(0);
                for (int j = 0; j < list.size(); j++) {
                    COUNT = COUNT.add(list.get(j).getBigDecimal("GWEC009"));
                }
                resultList.get(i).put("COUNT", COUNT);
            }
        }
        return new PageReData(page, resultList);
    }

    /**
     * 新增页面,获取默认值
     */
    @RequestMapping("/getAddDef")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public Dto getAddDef(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto outDto= Dtos.newDto();
        outDto.put("GWECDOCNO","CHD"+ DateUtils.formatDate(new Date(),"yyyyMMddSSSSS"));
        outDto.put("GWECDOCDATE", DateUtils.getDate("yyyy-MM-dd"));
        outDto.put("GWEC012",shiroUser.getORG_ID());
        outDto.put("NAME_ORG",shiroUser.getSysOrgT().getNAME());
        outDto.put("GWEC011",shiroUser.getID());
        outDto.put("NAME_USER",shiroUser.getNAME());
        return outDto;
    }

    /**
     * 编辑页面,获取回显数据
     */
    @RequestMapping("/getData")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public Dto getData(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto inDto=dtoWapper.getDto();
        inDto.put("ent", shiroUser.getDBC());
        List<Dto> resultList = gwei005Service.list(inDto);
        Dto outDto = resultList.get(0);
        if (outDto.getString("GWECPLANT").equals("")) {
            outDto.put("AZP02", "");
        } else {
            inDto.put("BIZ_CODE", outDto.getString("GWECPLANT"));
            Dto azpDto = null;
            try {
                azpDto = erpLogic.getERPService("getAzpFile",inDto);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }
            if(azpDto!=null){
                outDto.put("AZP02", azpDto.getString("AZP02"));
            }else{
                outDto.put("AZP02", "");
            }
        }
        return outDto;
    }

    /**
     * 编辑页面,获取回显数据 GwecData
     */
    @RequestMapping("/getGwecData")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public List<Dto> getGwecData(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto inDto=dtoWapper.getDto();
        inDto.put("ent", shiroUser.getDBC());
        List<Dto> resultDto = gwei005Service.listGwec(inDto);
        BigDecimal count = new BigDecimal(0);
        for (int i = 0; i < resultDto.size(); i++) {
//            if(StringUtils.isNotEmpty(resultDto.get(i).getString("GWEC003"))){
//                Dto eDto= Dtos.newDto();
//                eDto.put("OEB01",resultDto.get(i).getString("GWEC003"));//订单号
//                eDto.put("OEB03",resultDto.get(i).getBigDecimal("GWEC101"));//订单项次
//                eDto.put("DBC",resultDto.get(i).getString("GWECPLANT"));
//                Dto oeb =gwei005Service.getOeb(eDto);
//                if(oeb!=null){
//                    resultDto.get(i).put("GWCS002",oeb.getString("OEB05"));//单位
//                    resultDto.get(i).put("GWCS003",oeb.getBigDecimal("OEB12"));//数量
//                }
//            }
            if(StringUtils.isNotEmpty(resultDto.get(i).getString("GWEC003"))){
                Dto eDto= Dtos.newDto();
                eDto.put("GWEC003",resultDto.get(i).getString("GWEC003"));//订单号
                eDto.put("GWEC101",resultDto.get(i).getBigDecimal("GWEC101"));//订单项次
//                eDto.put("DBC",resultDto.get(i).getString("GWECPLANT"));
                Dto oeb =gwei005Service.getOeb(eDto);
                if(oeb!=null){
                    resultDto.get(i).put("GWCS002",oeb.getString("GWCS002"));//单位
                    resultDto.get(i).put("GWCS003",oeb.getBigDecimal("GWCS003"));//数量
                }
            }
            resultDto.get(i).put("ROW_ID", i + 1);
            Dto dto = Dtos.newDto();
            dto.put("GWEC", resultDto.get(i).getString("GWECDOCNO") + "-" + resultDto.get(i).getString("GWECSEQ"));
            dto.put("GWEC003", resultDto.get(i).getString("GWEC003"));
            dto.put("GWEC101", resultDto.get(i).getString("GWEC101"));
            List<Dto> list = gwei005Service.listC(dto);
            for (int j = 0; j < list.size(); j++) {
                count = count.add(list.get(j).getBigDecimal("GWEC009"));
            }
            resultDto.get(i).put("COUNT", count);
        }
        return resultDto;
    }

    /**
     * 获取默认联系人、到货地点、物流信息
     *
     * @return
     */
    @ResponseBody
    @RequiresPermissions("crm:gwei005:init")
    @RequestMapping("/getInfo")
    public ResponseData getInfo(@DtoParam DtoWapper dtoWapper) {
        Dto inDto=dtoWapper.getDto();
        ShiroUser shiroUser= ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        inDto.put("ent",shiroUser.getDBC());
        List<Dto> info_list = this.gwei005Service.getMaxDate(inDto);
        info_list.remove(null);
        if(info_list.size() > 0){
            outDto = info_list.get(0);
        }else {
            try {
                outDto = this.erpLogic.getERPService("getOcc",inDto);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        return ResponseData.success(outDto);
    }

    @RequestMapping("/getTreeData")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData getTreeData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto=dtoWapper.getDto();
        inDto.put("IS_DEL", "0");
        inDto.put("TYPE","2");

        List<Dto> resultDto = gwei005Service.listOrg_1(inDto);
        for(int i = 0; i < resultDto.size(); i ++){
            inDto.put("BIZ_CODE",resultDto.get(i).getString("BIZ_CODE"));
            Dto azpDto = null;
            try {
                azpDto = erpLogic.getERPService("getAzpFile",inDto);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }catch(Exception e){
                e.printStackTrace();
            }
            if(azpDto!=null){
                resultDto.get(i).put("AZP02",azpDto.getString("AZP02"));
            }else{
                resultDto.get(i).put("AZP02", "");
            }
        }
        return ResponseData.success(resultDto);
    }


    /**
     * 编辑框添加按钮选择销售订单开窗
     */
    @RequestMapping("/getOebData")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData getOebData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser= ShiroKit.getUser();
        inDto.put("ent", shiroUser.getDBC());

        if (!StringUtils.isEmpty(inDto.getString("OEB"))) {
            String[] str = inDto.getString("OEB").split(",");
            List<String> plist = Lists.newArrayList();
            for (String oeb : str) {
                plist.add(oeb);
            }
            inDto.put("OEB", plist);
        }else{
            inDto.put("OEB",null);
        }
        List<Dto> delDto = gwei005Service.listDelGwec(inDto);
        if(delDto!=null && delDto.size() > 0){
            inDto.put("delGwec", delDto);
        }else{
            inDto.put("delGwec", null);
        }
        List<Dto> resultDto = gwei005Service.listOebPage(inDto,page);
        return new PageReData(page, resultDto);
    }

    @RequestMapping("/add")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwecT> gwecTList = inDto.getReqList(GwecT.class);
        this.gwei005Service.add(gwecTList);
        return SUCCESS_TIP;

    }

    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto= dtoWapper.getDto();
        List<GwecT> gwecTList = inDto.getReqList(GwecT.class);
        this.gwei005Service.edit(gwecTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/delRow")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData delRow(GwecT gwecT) {
        gwei005Service.delRow(gwecT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delRows")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData delRows(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwecT> gwecTList = inDto.getReqList(GwecT.class);
        gwei005Service.delRows(gwecTList);
        return SUCCESS_TIP;
    }

    /**
     * 批量审核数据
     */
    @RequestMapping("/check")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData check(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwecT> gwecTList = inDto.getReqList(GwecT.class);
        Dto dto=gwei005Service.check(gwecTList);
        if(dto.getAppCode()==400){
            return ERROR_TIP;
        }
        return SUCCESS_TIP;
    }

    /**
     * 批量取消审核数据
     */
    @RequestMapping("/unCheck")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData unCheck(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwecT> gwecTList = inDto.getReqList(GwecT.class);
        Dto dto=gwei005Service.unCheck(gwecTList);
        if(dto.getAppCode()==400){
            return ERROR_TIP;
        }
        return SUCCESS_TIP;
    }

    /**
     * 打印
     * @param dtoWapper
     * @return
     */
    @RequestMapping("/print")
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public Dto initPrint(@DtoParam DtoWapper dtoWapper){
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser= ShiroKit.getUser();
        inDto.put("ent", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Dto> list = gwei005Service.getGwecBydoc(inDto);
        Dto dto = list.get(0);
        //获取公司名--start
        Dto queryDto = Dtos.newDto();
        queryDto.put("ERP_Link",inDto.getString("ERP_Link"));
        queryDto.put("GWECPLANT", dto.getString("GWECPLANT"));
        queryDto.put("ZO01", "2");
        Dto zoDto = null;
        try {
            zoDto = this.erpLogic.getERPService("getZo", queryDto);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (zoDto != null) {
            dto.put("ZO02",zoDto.getString("ZO02"));
        }else{
            dto.put("ZO02", "");
        }
        //获取公司名--end
        List<Dto> gwec_list = Lists.newArrayList();
        for(int i=0;i<list.size();i++){
            Dto qDto = list.get(i);
            qDto.put("ent", shiroUser.getDBC());
            qDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
            Dto resultDto = gwei005Service.getOea(qDto);
            resultDto.put("GWECSEQ",list.get(i).getBigDecimal("GWECSEQ"));
            resultDto.put("GWEC009",qDto.getString("GWEC009"));
            gwec_list.add(resultDto);
        }
        Dto outDto = Dtos.newDto();
        outDto.put("gwec",dto);
        outDto.put("gwec_list", gwec_list);
        return outDto;
    }


    @RequestMapping(value = "/importsData", method = RequestMethod.POST)
    @RequiresPermissions("crm:gwei005:init")
    @ResponseBody
    public ResponseData importsData(MultipartFile file, ModelMap map) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = Dtos.newDto();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);//表格标题行数,默认0
        params.setHeadRows(1);//表头行数,默认1
        params.setStartRows(0);//字段真正值和列标题之间的距离 默认0
        try {
            List<GwecT> list = ExcelImportUtil.importExcel(file.getInputStream(), GwecT.class, params);
            for (GwecT gwecT : list) {
                gwecT.setGWECDOCDATE(LocalDate.now());
                gwecT.setGWECENT(shiroUser.getDBC());
                gwecT.setGWECUSER(shiroUser.getACCOUNT());
                gwecT.setGWECGRUP(shiroUser.getORG_ID());
                gwecT.setGWECORIU(shiroUser.getACCOUNT());
                gwecT.setGWECORIG(shiroUser.getORG_ID());
                gwecT.setGWECTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));
                gwecT.setGWECDATE(LocalDate.now());
                gwecT.setGWECACTI("Y");
                gwecT.setGWECCONF("N");
                gwecT.setGWEC016("Y");
                gwecT.insert();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(BizExceptionEnum.SERVER_ERROR.getCode(),
                    BizExceptionEnum.SERVER_ERROR.getMessage());
        }
        return SUCCESS_TIP;
    }

}
